package gov.pbc.nn.kjgl.software.view;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import gov.pbc.nn.kjgl.entity.Project;
import gov.pbc.nn.kjgl.entity.ProjectFile;
import gov.pbc.nn.kjgl.entity.ProjectMember;
import gov.pbc.nn.kjgl.entity.ProjectTask;
import gov.pbc.nn.kjgl.software.dao.ProjectTaskDao;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.bstek.dorado.annotation.DataProvider;
import com.bstek.dorado.annotation.DataResolver;
import com.bstek.dorado.annotation.Expose;
import com.bstek.dorado.data.provider.Criteria;
import com.bstek.dorado.data.provider.Page;
import com.bstek.dorado.uploader.UploadFile;
import com.bstek.dorado.uploader.annotation.FileResolver;

@Component("projectTaskPR")
public class ProjectTaskPR {

	@Autowired
	private ProjectTaskDao projectTaskDao;

	/**
	 * 获取Project
	 * 
	 * @return
	 */
	@DataProvider
	public void loadProject(Page<Project> page, Criteria criteria) {
		projectTaskDao.loadProject(page, criteria);
	}

	/**
	 * 获取项目相关人员
	 * 
	 * @param parameter
	 * @return
	 */
	@DataProvider
	public List<Map<String, Object>> loadDropDown(Map<String, Object> parameter) {
		return projectTaskDao.loadDropDown(parameter);
	}

	/**
	 * 根据ProjectId获取任务
	 * 
	 * @param page
	 * @param projectId
	 */
	@DataProvider
	public void loadProjectTask(Page<ProjectTask> page, String projectId, Criteria criteria) {
		projectTaskDao.loadProjectTask(page, projectId, criteria);
	}

	/**
	 * 获取当前登录人的任务
	 * 
	 * @param page
	 * @param projectId
	 */
	@DataProvider
	public void loadMyProjectTask(Page<ProjectTask> page, String projectId, Criteria criteria) {
		projectTaskDao.loadMyProjectTask(page, projectId, criteria);
	}

	/**
	 * 根据ProjectTaskId获取对应需求书
	 * 
	 * @param projectId
	 * @return
	 */
	@DataProvider
	public List<ProjectFile> loadProjectFileByProjectTaskId(String projectId) {
		return projectTaskDao.loadProjectFileByProjectTaskId(projectId);
	}

	/**
	 * 维护任务管理
	 * 
	 * @param projectTask
	 * @throws IOException
	 */
	@Transactional
	@DataResolver
	public void saveProjectTask(ProjectTask projectTask, String id) throws IOException {
		projectTaskDao.saveProjectTask(projectTask, id);
	}

	@Transactional
	@DataResolver
	public void saveMyProjectTask(Collection<ProjectTask> projectTask) {
		for (ProjectTask p : projectTask) {
			projectTaskDao.update(p);
		}
	}

	/**
	 * 删除任务管理
	 * 
	 * @param list
	 */
	@Transactional
	@Expose
	public void deleteProjectTask(List<String> list) {
		projectTaskDao.deleteProjectTask(list);
	}

	/**
	 * 根据任务id获取对应的任务文件
	 * 
	 * @param taskId
	 * @return
	 */
	@Expose
	public List<String> loadTaskFileByTaskId(String taskId) {
		return projectTaskDao.loadTaskFileByTaskId(taskId);
	}

	@Transactional
	@DataResolver
	public void deleteProjectTaskFile(List<ProjectFile> list) throws IOException {
		if (list != null) {
			for (ProjectFile pf : list) {
				projectTaskDao.delete(pf);
			}
			for (ProjectFile pf : list) {
				String file = pf.getPath();
				Files.deleteIfExists(Paths.get(file));
			}
		}

	}

	/**
	 * 上传附件
	 * 
	 * @param file
	 * @param parameter
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unused")
	@FileResolver
	public Object process2(UploadFile file, Map<String, Object> parameter) throws Exception {
		String problemId = (String) parameter.get("problemId");
		if (StringUtils.isEmpty(problemId)) {
			// 问题数据尚未保存，需要将上传文件保存到临时目录中，以后当问题数据保存的时候将临时目录中的该文件转移到实际目录中，并更新文件地址
			Path tmpFile = Files.createTempFile(UUID.randomUUID().toString(), null);
			Files.copy(file.getInputStream(), tmpFile, StandardCopyOption.REPLACE_EXISTING);
			return tmpFile.toString();
		} else {
			Path dest = this.projectTaskDao.getRealPath();// 上传文件保存后的实际地址

			// 在实际将文件保存到实际地址前要进行数据库操作，这是为了实现文件保存到磁盘时出现异常时能够使数据库事务回滚。
			ProjectFile pf = new ProjectFile();
			pf.setOwnerId(problemId);
			pf.setFileName(file.getFileName());
			pf.setPath(dest.toString());
			pf.setType(2);
			this.projectTaskDao.save(pf);

			Files.copy(file.getInputStream(), dest);
			return pf;
		}
	}
}